//*************************************************************************//
//*           _____   ____________            ______               __     *//
//*          / /__ \ / ____/ ____/           / ____/___  _________/ /     *//
//*     __  / /__/ // __/ / __/    ______   / /_  / __ \/ ___/ __  /      *//
//*    / /_/ // __// /___/ /___   /_____/  / __/ / /_/ / /  / /_/ /       *//
//*    \____//____/_____/_____/           /_/    \____/_/   \__,_/        *//
//*                                                                       *//
//*     Copyright (c) 2006 Ford Argentina. All Rights Reserved.           *//
//*                                                                       *//
//*************************************************************************//

package ar.com.ford.it.webengine.websession;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import ar.com.ford.it.webengine.helper.FordAppDateFactory;
import ar.com.ford.it.webengine.security.ISecurityProvider;
import ar.com.ford.it.webengine.security.SecurityProviderFactory;
import ar.com.ford.it.webengine.settings.AppSettings;
import ar.com.ford.it.webengine.wslx.WslxUser;
import ar.com.ford.it.webengine.wslx.WslxUserFactory;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;

public class WebSessionHelper {

	private static final String CLASS_NAME = WebSessionHelper.class.getName();
	private static final ILogger log = LogFactory.getInstance().getLogger(CLASS_NAME);

	private static final String WEBSESSION_ID_PREFIX = "ws";
	public static final String WSLXUSER_ID = "wslxuser";
	
	private WslxUser createWslxUser(String userId) {
		String METHOD_NAME = "createWslxUser";
		
		log.entering(CLASS_NAME, METHOD_NAME);
		WslxUser retorno = null;

		retorno = WslxUserFactory.getWslxUser(userId);		
		
		log.exiting(CLASS_NAME, METHOD_NAME);
		return retorno;
	}
	
	 
    public WebSession registerSession(HttpServletRequest request, String userId) {
    	String METHOD_NAME = "registerSession";
    	
    	log.entering(CLASS_NAME, METHOD_NAME);
    	HttpSession httpSession = request.getSession();

    	WslxUser wslxUser = this.createWslxUser(userId);
    	
    	if (wslxUser == null) {
    		log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "No se pudo crear wslxuser");
    		return null;
    	}
        
    	ISecurityProvider secProvider = SecurityProviderFactory.getSecurityProvider();
    	wslxUser.setProfiles(secProvider.buildProfiles(AppSettings.getInstance().getAPP_NAME(), wslxUser));
    	wslxUser.setPermissions(secProvider.buildPermissions(AppSettings.getInstance().getAPP_NAME(), wslxUser));
    	wslxUser.setMenuAccess(secProvider.buildmenuAccess(AppSettings.getInstance().getAPP_NAME(), wslxUser));
    	
    	
        AppSettings ac = AppSettings.getInstance();
		
        WebSession websession = new WebSession(request.getRemoteAddr(), wslxUser, ac.getAPP_NAME());

		// inicializa lastRequest
        websession.setLastRequest(FordAppDateFactory.getAppDate());
        
        httpSession.setAttribute(WEBSESSION_ID_PREFIX+httpSession.getId(), websession);
		httpSession.setAttribute(WSLXUSER_ID, websession.getUser());
        
		httpSession.setMaxInactiveInterval(ac.getAPP_SESSION_TIME_OUT().intValue());

        
        log.exiting(CLASS_NAME, METHOD_NAME);
        
        return websession;
    }
    
    public static WebSession getWebSession(HttpSession httpSession) {
    	return (WebSession)httpSession.getAttribute(WEBSESSION_ID_PREFIX+httpSession.getId());
    }
   
    public static WebSession removeWebSession(HttpSession httpSession) {
    	WebSession result = getWebSession(httpSession);
    	if (result != null) 
    		httpSession.removeAttribute(WEBSESSION_ID_PREFIX+httpSession.getId());
    	
    	return result;
    }
}
